package com.liliudong.rommath.api.sys.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.liliudong.rommath.biz.sys.entity.SysMenu;
import com.liliudong.rommath.biz.sys.param.SysMenuAddParam;
import com.liliudong.rommath.biz.sys.param.SysMenuModifyParam;
import com.liliudong.rommath.biz.sys.result.SysMenuChildrenResult;
import com.liliudong.rommath.biz.sys.result.SysMenuDetailResult;
import com.liliudong.rommath.biz.sys.service.SysMenuService;
import com.liliudong.rommath.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单 控制层。
 *
 * @author liudong.li
 * @since 1.0.0
 */
@RestController
@RequestMapping("/sys/menu")
@RequiredArgsConstructor
public class SysMenuController {

    private final SysMenuService sysMenuService;

    /**
     * 添加
     *
     * @param param param
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:menu:add")
    @PostMapping
    public R<?> add(@RequestBody @Validated SysMenuAddParam param) {
        sysMenuService.add(param);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param id 身份证件
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:menu:delete")
    @DeleteMapping("/{id}")
    public R<?> delete(@PathVariable Long id) {
        sysMenuService.deleteById(id);
        return R.ok();
    }

    /**
     * 修改
     *
     * @param param param
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:menu:modify")
    @PutMapping
    public R<?> modify(@RequestBody @Validated SysMenuModifyParam param) {
        sysMenuService.modify(param);
        return R.ok();
    }

    /**
     * 路由
     *
     * @return {@link R }<{@link ? }>
     */
    @SaCheckLogin
    @GetMapping("/routes")
    public R<List<Tree<Long>>> routes() {
        List<Tree<Long>> routes = sysMenuService.routes();
        return R.ok(routes);
    }

    /**
     * 详情
     *
     * @param id 身份证件
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:menu:query")
    @GetMapping("/detail/{id}")
    public R<SysMenuDetailResult> detail(@PathVariable Long id) {
        SysMenuDetailResult result = sysMenuService.detailById(id);
        return R.ok(result);
    }

    /**
     * 子级
     *
     * @param id 身份证件
     * @return {@link R }<{@link List }<{@link SysMenuChildrenResult }>>
     */
    @SaCheckPermission("sys:menu:query")
    @GetMapping("/children")
    public R<List<SysMenuChildrenResult>> children(Long id) {
        List<SysMenuChildrenResult> children = sysMenuService.children(id);
        return R.ok(children);
    }

    /**
     * 树形
     *
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:menu:query")
    @GetMapping("/tree")
    public R<List<Tree<Long>>> tree(@RequestParam(required = false) List<SysMenu.MenuType> excludeTypes) {
        List<Tree<Long>> tree = sysMenuService.tree(excludeTypes);
        return R.ok(tree);
    }

}